import java.io.*;

public class adb {

    public static void main(String[] args) throws IOException {

        File F=new File("C://Users/13051/Desktop/file.txt");
        //如果文件不存在,就动态创建文件
        if(!F.exists()){
            F.createNewFile();
        }
        FileWriter fw=null;

        String cmd = "adb version";
        Process process = Runtime.getRuntime().exec(cmd);
        try {
            ByteArrayOutputStream resultOutStream = new ByteArrayOutputStream();
            InputStream errorInStream = new BufferedInputStream(process.getErrorStream());
            InputStream processInStream = new BufferedInputStream(process.getInputStream());
            int num = 0;
            byte[] bs = new byte[1024];
            while ((num = errorInStream.read(bs)) != -1) {
                resultOutStream.write(bs, 0, num);
            }
            while ((num = processInStream.read(bs)) != -1) {
                resultOutStream.write(bs, 0, num);
            }
            String result = resultOutStream.toString();
            System.out.println(result);
            errorInStream.close();
            processInStream.close();
            resultOutStream.close();

            //设置为:True,表示写入的时候追加数据
            fw=new FileWriter(F, true);
            //回车并换行
            fw.write(result+"\r\n");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            process.destroy();
            assert fw != null;
            fw.close();
        }
    }
}
